<?php

namespace App\Http\Controllers\Admin;

use App\Models\Cate;
use App\Models\Member;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Article as ArticleInfo;

class Article extends Controller
{
    //文章列表
    public function alls ()
    {
        $articles = (new ArticleInfo)->with('members','cates')->orderBy('is_top')->orderBy('status')->latest()->paginate(10);

        $data = [
            'articles' => $articles,
        ];
        return view('admin.article.list',$data);
    }

    //文章添加
    public function add ()
    {
        if (request()->isMethod('post')) {
            // $data = request()->only(['title','tags','member_id','cate_id','is_top','status','desc','content']);
            $data = [
                'title' => request('title'),
                'tags' => request('tags'),
                'member_id' => request('member_id'),
                'cate_id' => request('cate_id'),
                'is_top' => request()->input('is_top','0'),
                'status' => request()->input('status','0'),
                'desc' => request('desc'),
                'content' => request('content')
            ];

            $result = (new ArticleInfo)->add($data);
            if ($result == 1) {
                $msg = [
                    'code' => 1,
                    'msg' => "添加成功",
                    'url' => url('admin/articlelist')
                ];
            } else {
                $msg = [
                    'code' => 0,
                    'msg' => $result
                ];
            }

            return $msg;
        }

        $members = Member::all();
        $cates = Cate::all();
        $datas = [
            'members' => $members,
            'cates' => $cates
        ];
        return view('admin.article.add',$datas);
    }

    //文章删除
    public function del ()
    {
        if (request()->isMethod('post')) {
            $articles = ArticleInfo::find(request('id'));
            $result = $articles->delete();
            if ($result) {
                $msg = [
                    'code' => 1,
                    'msg' => "删除成功",
                    'url' => url('admin/articlelist')
                ];
            } else {
                $msg = [
                    'code' => 0,
                    'msg' => "删除失败"
                ];
            }

            return $msg;
            }   
    }

    //文章上架 下架
    public function stus ()
    {
        if (request()->isMethod('post')) {
            $data = [
                'id' => request('id'),
                'status' => request('status') ? '0' : 1,
            ];

            $stu = ArticleInfo::find($data['id']);
            
            $stu->status = $data['status'];
            $result = $stu -> save();
            if ($result) {
                $msg = [
                    'code' => 1,
                    'msg' => "操作成功",
                    'url' => url('admin/articlelist')
                ];
            } else {
                $msg = [
                    'code' => 0,
                    'msg' => "操作失败"
                ];
            }
            return $msg;

        }
    }

    //文章置顶操作
    public function istop ()
    {
        if (request()->isMethod('post')) {
            $data = [
                'id' => request('id'),
                'is_top' => request('is_top') ? '0' : 1,
            ];

            $top = ArticleInfo::find($data['id']);
            $top->is_top = $data['is_top'];
            $result = $top->save();
            if ($result) {
                $msg = [
                    'code' => 1,
                    'msg' => "操作成功",
                    'url' => url('admin/articlelist'),
                ];
            } else {
                $msg = [
                    'code' => 0,
                    'msg' => "操作失败"
                ];
            }
            return $msg;
        }
    }
}
